<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-07-08 10:27:01
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\BaseData;

use App\Repositories\MaterialRepository;
use App\Repositories\MpcLotRepository;
use App\Repositories\MaterialCategoryRepository;
use App\Http\Services\Service;
use Illuminate\Support\Facades\DB;

/**
 * @property MaterialRepository $repository
 * @property MaterialCategoryRepository $category
 * @property MpcLotRepository $lot
 */
class MaterialService extends Service
    {
    public function boot(MaterialRepository $repository, MaterialCategoryRepository $category, MpcLotRepository $lot)
        {
        $this->repository = $repository;
        $this->category   = $category;
        $this->lot        = $lot;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    protected function save($request)
        {
        // DB::enableQueryLog();
        // dd(DB::getQueryLog());
        if (app("sys")->is_inspect_factory === 1) {
            fail("开启验厂情况下，禁止编辑物料！！");
            }
        $category = $this->category->whereId($request->input("category_id"))->first();
        $data     = $request->toArray();

        if (!isEmpty($data['id'])) {
            $this->repository->where("id", "<>", $data['id']);
            }
        if (!is_numeric($data['weight'])) {
            fail("克重{$data['weight']}必须填数字！");
            }
        if (isNotEmpty($data['code'])) {
            $material = $this->repository->whereCode($data['code'])->first();
            if ($material) {
                fail("{$data['code']}编码已存在！");
                }
            }
        if (isNotEmpty($data['fullname'])) {
            $material = $this->repository->whereCode($data['fullname'])->first();
            if ($material) {
                fail("{$data['fullname']}全称已存在！");
                }
            }
        $data['material_nature_id'] = $category->material_nature_id;
        $acsign                     = 'MATERIAL_CODE4';

        if (in_array($category->name, ['膜'])) {
            $acsign = 'MATERIAL_CODE';
            }

        if (in_array($category->name, ['油胶', '热熔胶'])) {
            $acsign = 'MATERIAL_CODE3';
            }
        if (in_array($category->parent->name, ['底布'])) {
            $acsign = 'MATERIAL_CODE2';
            }
        if (!isset($data['coefficient']) || isEmpty($data['coefficient'])) {
            $data['coefficient'] = 1;
            }
        return $this->repository->setACSign($acsign)->autosave($data);
        }
    protected function delete($request)
        {
        return $this->repository->delete($request->input("ids"));
        }

    protected function deleteCategory()
        {
        $this->category->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->children->count() > 0) {
                fail("存在子分组！删除失败！");
                }
            $v->delete();
            });
        }
    public function lot()
        {

        $data = $this->repository->condition(request())->first();
        // DB::enableQueryLog();
        $list = $this->lot->whereMaterial_id($data->id)->getGrid();
        // dd(DB::getQueryLog());
        return $list;

        }
    public function listLcategory()
        {
        return $this->category->whereLcategory_id(1)->get();
        }
    public function list_without_tree()
        {
        return $this->category->condition()->get();
        }
    }